//
//  DetailTableView.m
//  Cat
//
//  Created by lushitong on 2018/12/23.
//  Copyright © 2018年 lushitong. All rights reserved.
//

#import "DetailTableView.h"

@implementation DetailTableView

- (instancetype)initWithFrame:(CGRect)frame sections:(NSInteger)sections {
	if (self = [super initWithFrame:frame sections:sections]) {
		self.backgroundColor = [UIColor whiteColor];
		self.tableView.backgroundColor = [UIColor whiteColor];
		self.noFirstAutoRequest = YES;
		self.outerControlLoading = YES;
		self.addRefresh = NO;
		self.tableView.showsVerticalScrollIndicator = NO;
	}
	return self;
}

- (void)setModel:(ProductInfo *)model {
	_model = model;
	[self loadAllView];
	[self.tableView reloadData];
}

- (void)loadAllView {
	[self p_setupBannerCell];
	[self p_setInfoCell];
	[self p_setChekShopCell];
	[self p_setwebCell];
}

- (void)p_setupBannerCell {
	QKBaseTableCellModel *accountCellData = [[QKBaseTableCellModel alloc] init];
	accountCellData.isDynamic = NO;
	accountCellData.cellClass = @"DetailTopCell";
	accountCellData.section = 0;
	accountCellData.cellData = self.model;
	accountCellData.cellHeight = ScreenWidth + kQKFitModuleDecimal(40.0);
	[self addTableSectionData:accountCellData];
}


- (void)p_setInfoCell {
	QKBaseTableCellModel *accountCellData = [[QKBaseTableCellModel alloc] init];
	accountCellData.isDynamic = NO;
	accountCellData.cellClass = @"DetailInfoCell";
	accountCellData.section = 1;
	accountCellData.cellData = self.model;
	accountCellData.cellHeight = kQKFitModuleDecimal(149);
	[self addTableSectionData:accountCellData];
}

- (void)p_setChekShopCell {
	QKBaseTableCellModel *accountCellData = [[QKBaseTableCellModel alloc] init];
	accountCellData.isDynamic = YES;
	accountCellData.cellClass = @"DetailsCheckShopCell";
	accountCellData.section = 2;
	accountCellData.cellData = @[@"",@""];
	accountCellData.cellHeight = kQKFitModuleDecimal(50);
	[self addTableSectionData:accountCellData];
}

- (void)p_setwebCell {
	QKBaseTableCellModel *accountCellData = [[QKBaseTableCellModel alloc] init];
	accountCellData.isDynamic = NO;
	accountCellData.cellClass = @"DetailsWebCell";
	accountCellData.section = 3;
	accountCellData.cellData = self.model;
	accountCellData.cellHeight = self.model.detail.count * ScreenWidth;
	[self addTableSectionData:accountCellData];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	UITableViewCell *sectionCell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
	if (indexPath.section == 2) {
		if (indexPath.row == 0) {
			sectionCell = [self cellWithoutAnything];
		}
	}
	return sectionCell;
}

- (UITableViewCell *)cellWithoutAnything {
	static NSString *identifier = @"QKProfileNothingCell";
	UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:identifier];
	if (!cell) {
		cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
	}
	cell.selectionStyle = UITableViewCellSelectionStyleNone;
	cell.contentView.backgroundColor = COLOR_WITH_HEX(0xF3F3F3);
	return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
	CGFloat cellHeight  = [super tableView:tableView heightForRowAtIndexPath:indexPath];
	if (indexPath.section == 2) {
		if (indexPath.row == 0) {
			cellHeight = kQKFitModuleDecimal(8);
		} else {
			cellHeight = kQKFitModuleDecimal(50);
		}
	}
	return cellHeight;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	[tableView deselectRowAtIndexPath:indexPath animated:YES];
	if (indexPath.row == 1) {
		[self showItemInTaobao4iOS:self.productID];
	}
}

- (void)showItemInTaobao4iOS:(NSNumber *)productID {
	// 判断当前系统是否有安装淘宝客户端
	NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"taobao://item.taobao.com/item.htm?id=%@", productID.stringValue]];
	if ([[UIApplication sharedApplication] canOpenURL:url]) {
		[[UIApplication sharedApplication] openURL:url options:nil completionHandler:^(BOOL success) {
			
		}];
	} else {
		url = [NSURL URLWithString:[NSString stringWithFormat:@"http://item.taobao.com/item.htm?id=%@", productID.stringValue]];
		[[UIApplication sharedApplication] openURL:url];
	}
}

@end
